package yicheng.dinnertogetherapp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;
import yicheng.dinnertogetherapp.R;


public class StartingViewActivity extends Activity{

	private AmazonClientManager clientManager;
	public static List<String> domainNames;
	private Handler handler;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		clientManager = new AmazonClientManager();


		setContentView(R.layout.starting_view_layout);
		
		if (isConnectedToInternet()){
			//load in database domain names
			loadDataBase();

		}
		else{
			Toast.makeText(getApplicationContext(), "You are not conneceted to Internet, please check.", Toast.LENGTH_LONG).show();
			Handler newHandler = new Handler();
			newHandler.postDelayed(new Runnable(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					finish();
					overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
				}
				
			}, 1000);
		}
		
		
		
		
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 1){

					Intent main_view_intent = new Intent("yicheng.dinnertogetherapp.FIRSTLAUNCHACTIVITY");
					overridePendingTransition(R.anim.activity_left_in, R.anim.activity_left_out);
					startActivity(main_view_intent);

				}
			}
		}; 

		/*	handler.postDelayed(new Runnable() { 
			public void run() { 
				Intent main_view_intent = new Intent("yicheng.dinnertogetherapp.FIRSTLAUNCHACTIVITY");
				overridePendingTransition(R.anim.activity_left_in, R.anim.activity_left_out);
				startActivity(main_view_intent);
			} 
		}, 1500); */



	}

	private void loadDataBase(){
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						domainNames = SimpleDB.getDomainNames(clientManager);


						//loadingSpinner.setVisibility(View.VISIBLE);


						/*	}
						catch (NullPointerException e){
							Toast.makeText(getApplicationContext(), "Your connection to Internet has problem, please check.", Toast.LENGTH_SHORT).show();
						}*/
						Message msg = Message.obtain();
						msg.what = 1;
						handler.sendMessage(msg);

					}

				}).start();
			}

		});

	}

	private boolean isConnectedToInternet(){
		ConnectivityManager connectivityManager = 
				(ConnectivityManager)StartingViewActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo wifi_network = 
				connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo mobile_network = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


		return wifi_network.isConnectedOrConnecting() || mobile_network.isConnectedOrConnecting();

		/*return mobile_network.isConnectedOrConnecting();*/
	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();

	}

}
